package com.loukou.lkt.lkt.dao;

import java.util.List;

import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.transaction.annotation.Transactional;

import com.loukou.lkt.lkt.entity.GoodsStock;

public interface GoodsStockDao extends CrudRepository<GoodsStock,Integer>{
	
	List<GoodsStock> findByWarehouseId(int warehouseId);
	
	List<GoodsStock> findByGoodsId(int goodsId);
	
	List<GoodsStock> findByWarehouseIdAndPositionTypeAndStockStateAndGoodsIdIn(int warehouseId, int positionType, int stockState, List<Integer> goodsIds);
	
	List<GoodsStock> findByWarehouseIdAndPositionTypeAndStockState(int warehouseId,int positionType,int stockState);
	
	List<GoodsStock> findByWarehouseIdAndPositionType(int warehouseId,int positionType);
	
	GoodsStock findByWarehouseIdAndPositionTypeAndGoodsIdAndStockState(int warehouseId,int positionType,int goodsId,int stockState);
	
	List<GoodsStock> findByWarehouseIdAndPositionTypeAndGoodsIdInAndStockState(int warehouseId,int positionType,List<Integer> goodsIds,int stockState);
	
	List<GoodsStock> findByWarehouseIdAndPositionTypeAndGoodsIdIn(int warehouseId,int positionType,List<Integer> goodsIds);
	
	@Modifying
	@Transactional
	@Query("update GoodsStock set stock = stock+?5 where warehouseId = ?1 and positionType = ?2 and stockState = ?3 and goodsId = ?4")
	void plusStock(int warehouseId,int positiontype,int stockState,int goodsId, int stock);
	
	@Modifying
	@Transactional
	@Query("update GoodsStock set stock = stock-?5 where warehouseId = ?1 and positionType = ?2 and stockState = ?3 and goodsId = ?4")
	void minusStock(int warehouseId,int positiontype,int stockState,int goodsId, int stock);

	@Modifying
	@Transactional
	@Query("update GoodsStock set stock = stock+?5 where id=?2")
	void plusStock(int diffNum, int id);

	@Modifying
	@Transactional
	@Query("update GoodsStock set stock = stock-?5 where id=?2")
	void minusStock(int diffNum, int id);
	
}
